package yqt.com.cnki.dao.impl;

import cnki.tpi.base.DataContext;
import cnki.tpi.kbatis.sqlsession.SqlSession;
import cnki.tpi.kbatis.utils.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import yqt.com.cnki.bo.DataBaseBo;
import yqt.com.cnki.dao.DIYTagDao;
import yqt.com.cnki.po.DataBasePo;
import yqt.com.cnki.po.SYS_CMS_OPTION_Po;
import yqt.com.cnki.po.SubmitFieldPo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class DIYTagDaoImpl implements DIYTagDao {
    @Autowired
    private DataContext dataContext;

    @Override
    public List<Map<String, String>> getAllDBbase() {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "yqt.com.cnki.dao.DIYTagDao.getAllDBbase";
        List<Map<String, String>> res = sqlSession.selectList(statementId, null, false);
        return res;
    }

    @Override
    public List<DataBaseBo> cutList(DataBaseBo dataBaseBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "yqt.com.cnki.dao.DIYTagDao.cutList";
        List<DataBaseBo> res = sqlSession.selectList(statementId, dataBaseBo, false);
        return res;
    }

    @Override
    public int cutCount(DataBaseBo dataBaseBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "yqt.com.cnki.dao.DIYTagDao.cutCount";
        Number num = sqlSession.getCount(statementId,dataBaseBo);
        return num.intValue();
    }

    @Override
    public Boolean isDataBaseUpdated(DataBaseBo dataBaseBo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "yqt.com.cnki.dao.DIYTagDao.isDataBaseUpdated";
        Long num = sqlSession.getCount(statementId,dataBaseBo);
        if (num > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<SubmitFieldPo> getNotSysFieldsState(String DBcode) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "yqt.com.cnki.dao.DIYTagDao.getNotSysFieldsState";
        List<SubmitFieldPo> res = sqlSession.selectList(statementId, DBcode, false);
        return res;
    }

    @Override
    public List<SubmitFieldPo> getSubmitFiled(String DBcode) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "yqt.com.cnki.dao.DIYTagDao.getSubmitFiled";
        List<SubmitFieldPo> res = sqlSession.selectList(statementId, DBcode, false);
        return res;
    }

    @Override
    public List<SYS_CMS_OPTION_Po> getAllShowToCustomer() {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "yqt.com.cnki.dao.DIYTagDao.getAllShowToCustomer";
        List<SYS_CMS_OPTION_Po> res = sqlSession.selectList(statementId, null, false);
        return res;
    }

    @Override
    public Boolean showToCustomerChange(DataBasePo dataBasePo) {
        Boolean newState = dataBasePo.getShowToCustomer();
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId;
        if (newState.equals(true)){
            statementId = "yqt.com.cnki.dao.DIYTagDao.addShowToCustomer";

        }else{
            statementId = "yqt.com.cnki.dao.DIYTagDao.deleteShowToCustomer";
        }
        return sqlSession.executeInsert(statementId, dataBasePo);

    }

    @Override
    public Boolean save(String dbCode,ArrayList<SubmitFieldPo> params) {
        Boolean res = true;

        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "yqt.com.cnki.dao.DIYTagDao.deleteOldFields";
        Boolean deleteRes = sqlSession.executeDelete(statementId, dbCode);

        if (deleteRes){
            for (SubmitFieldPo o :params) {
                statementId = "yqt.com.cnki.dao.DIYTagDao.insertNewFiled";
                Boolean insertRes = sqlSession.executeInsert(statementId, o);
                if (!insertRes){
                    res = false;
                    break;
                }
            }
        }else{
            res = false;
        }
        return res;
    }
}
